package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.AuthTeacherClassify;
import com.xmy.cultivate.entity.RadeFiducialAdd;
import com.xmy.cultivate.entity.RadeFiducialSet;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.service.IAuthTeacherClassifyService;
import com.xmy.cultivate.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 教师分类 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-05-26
 */
@RestController
@RequestMapping("/api/authTeacherClassify")
public class AuthTeacherClassifyController {

    @Autowired
    IAuthTeacherClassifyService iAuthTeacherClassifyService;

    @GetMapping("")
    public Result findAll(
            @RequestParam(value = "teacherName", required = false) String teacherName,
            @RequestParam(value = "type", required = false) Integer type
    ) {
        QueryWrapper<AuthTeacherClassify> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*,getStaffNameForId(teacher_id) AS teacher_name ");
        if (StringUtils.isNotBlank(teacherName)) {
            queryWrapper.having("teacher_name like '%" + teacherName + "%'");
        }
        if (Objects.nonNull(type)) {
            queryWrapper.eq("type", type);
        }
        List<AuthTeacherClassify> authTeacherClassifies = iAuthTeacherClassifyService.list(queryWrapper);

        return new Result(Code.OK, authTeacherClassifies, Code.GET_OK_MSG);
    }

    @GetMapping("/getTeacherByClassify")
    public Result getTeacherByClassify(
            @RequestParam(value = "isRepetition", required = false, defaultValue = "0") Integer isRepetition,// 是否允许重复，0否1是
            @RequestParam(value = "roleType", required = false) Integer roleType, // 1校长 2老师
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "schoolId", required = false) String schoolId,
            @RequestParam(value = "teacherName", required = false) String teacherName
    ) {

        List<Staff> staffList = iAuthTeacherClassifyService.getTeacherByClassify(isRepetition, roleType, type, schoolId, teacherName);

        return new Result(Code.OK, staffList, Code.GET_OK_MSG);
    }

    @PostMapping()
    public Result save(@RequestBody AuthTeacherClassify authTeacherClassify) {
        for (Long teacherId : authTeacherClassify.getTeacherIds()) {
            AuthTeacherClassify teacherClassify = new AuthTeacherClassify();
            teacherClassify.setTeacherId(teacherId);
            teacherClassify.setType(authTeacherClassify.getType());
            iAuthTeacherClassifyService.save(teacherClassify);
        }

        return new Result(Code.OK, "", Code.SAVE_OK_MSG);
    }

    @DeleteMapping("")
    public Result delAccount(@RequestParam(value = "idStr") String idStr) {
        String[] ids = idStr.split(",");
        for (String id : ids) {
            AuthTeacherClassify authTeacherClassify = iAuthTeacherClassifyService.getById(id);
            if (Objects.isNull(authTeacherClassify)) {
                return new Result(Code.EXP_ERROR, null, "该数据不存在");
            }

            iAuthTeacherClassifyService.removeById(id);
        }

        return new Result(Code.OK, "", Code.DELETE_OK_MSG);
    }

}
